package cn.stylefeng.guns.modular.mobile.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.*;
import cn.stylefeng.guns.modular.manager.model.params.*;
import cn.stylefeng.guns.modular.manager.model.result.FlowGoodsPriceResult;
import cn.stylefeng.guns.modular.manager.model.result.FlowGoodsResult;
import cn.stylefeng.guns.modular.manager.service.*;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.FlowDeptPhones;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.FlowDeptPhonesService;
import cn.stylefeng.guns.utils.*;
import cn.stylefeng.guns.utils.douyin.DouyinHttpUtil;
import cn.stylefeng.guns.utils.douyin.DouyinResPhone;
import cn.stylefeng.guns.utils.map.*;
import cn.stylefeng.guns.utils.weixin.Jscode2Session;
import cn.stylefeng.guns.utils.weixin.WeiXinUtil;
import cn.stylefeng.guns.utils.weixin.WxPhoneData;
import cn.stylefeng.roses.core.util.MD5Util;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.util.TextEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 基础插件
 */
@Slf4j
@Controller
@RequestMapping("/mobile/api/base")
public class MobileBaseController {
    @Autowired
    FlowNewsTypeService flowNewsTypeService;
    @Autowired
    FlowNewsService flowNewsService;
    @Autowired
    FlowNewsProblemService flowNewsProblemService;
    @Autowired
    FlowNewsCaseService flowNewsCaseService;
    @Autowired
    FlowUserguideTypeService flowUserguideTypeService;
    @Autowired
    FlowUserguideService flowUserguideService;
    @Autowired
    FlowAboutService flowAboutService;
    @Autowired
    FlowStoreCityService flowStoreCityService;
    @Autowired
    FlowStoreCityNoteService flowStoreCityNoteService;
    @Autowired
    FlowConfigService flowConfigService;
    @Autowired
    FlowMemberCommentService flowMemberCommentService;
    @Autowired
    FlowBannerService flowBannerService;
    @Autowired
    FlowZfbMemberService flowZfbMemberService;
    @Autowired
    FlowBdMemberService flowBdMemberService;
    @Autowired
    FlowWxMemberService flowWxMemberService;

    @Autowired
    FlowPhoneCodeService flowPhoneCodeService;
    @Autowired
    FlowProblemTypeService flowProblemTypeService;
    @Autowired
    FlowMemberCouponActivityService flowMemberCouponActivityService;
    @Autowired
    FlowMemberCouponActivityRecordService flowMemberCouponActivityRecordService;
    @Autowired
    FlowCouponService flowCouponService;
    @Autowired
    FlowServiceService flowServiceService;
    @Autowired
    FlowServiceTypeService flowServiceTypeService;
    @Autowired
    FlowCityService flowCityService;
    @Autowired
    FlowFloorLevelService flowFloorLevelService;
    @Autowired
    FlowStorageDateService flowStorageDateService;
    @Autowired
    FlowCountryService flowCountryService;
    @Autowired
    FlowCountryCityService flowCountryCityService;
    @Autowired
    FlowGoodsTypeService flowGoodsTypeService;
    @Autowired
    FlowGoodsService flowGoodsService;
    @Autowired
    FlowServiceTypePriceService flowServiceTypePriceService;
    @Autowired
    FlowServiceDistancePriceService flowServiceDistancePriceService;
    @Autowired
    FlowElevatorPriceService flowElevatorPriceService;
    @Autowired
    FlowGoodsPriceService flowGoodsPriceService;
    @Autowired
    FlowStoreRegionService flowStoreRegionService;

    @Autowired
    FlowStoreRegionPriceService flowStoreRegionPriceService;

    //发送生成订单的短信
    @Autowired
    protected FlowDeptPhonesService flowDeptPhonesService;
    @Autowired
    protected DeptService deptService;
    @Autowired
    protected FlowServiceAreaPriceService flowServiceAreaPriceService;

    @Autowired
    FlowDouyinMemberService flowDouyinMemberService;

    /**
      会员类型,抖音
     * */
    private int MEMBER_TYPE_DOUYIN=3;

    @RequestMapping("/banners")
    @ResponseBody
    public ResponseData banners(){
        QueryWrapper<FlowBanner> flowBannerQueryWrapper = new QueryWrapper<>();
        flowBannerQueryWrapper.orderByAsc("orders");
        List<FlowBanner> flowBanners = flowBannerService.list(flowBannerQueryWrapper);
        for (int i = 0;i<flowBanners.size();i++){
            flowBanners.get(i).setBannerUrl(AppUtil.formatUrl(flowBanners.get(i).getBannerUrl()));
        }
        return ResponseData.success(flowBanners);
    }

    //查询资讯分类
    @RequestMapping("/newTypes")
    @ResponseBody
    public ResponseData newTypes(){
        QueryWrapper<FlowNewsType> flowNewsTypeQueryWrapper = new QueryWrapper<>();
        flowNewsTypeQueryWrapper.orderByAsc("orders");
        return ResponseData.success(flowNewsTypeService.list(flowNewsTypeQueryWrapper));
    }

    //查询资讯中心
    @RequestMapping("/news")
    @ResponseBody
    public ResponseData news(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        int pagesize = baseRequest.getInt("pagesize",10);
        int cpage = baseRequest.getInt("cpage",1);
        long newsType = baseRequest.getLong("newsType");
        //
        QueryWrapper<FlowNews> flowNewsQueryWrapper = new QueryWrapper<>();
        if(newsType != -1){
            flowNewsQueryWrapper.eq("news_type_id",newsType);
        }
        flowNewsQueryWrapper.orderByDesc("create_time");

        IPage page = new Page<FlowNews>(cpage,pagesize);
        page = flowNewsService.getBaseMapper().selectPage(page,flowNewsQueryWrapper);
        //
        List<FlowNews> list = page.getRecords();
        for (int i = 0;i<list.size();i++){
            list.get(i).setCover(AppUtil.formatUrl(list.get(i).getCover()));
            list.get(i).setDetail(list.get(i).getDetail().replace("\"/ueditor/","\""+AppUtil.domains+"/ueditor/"));
        }
        //图片转换
        return ResponseData.success(list);
    }

    @RequestMapping("/newsDetail")
    @ResponseBody
    public ResponseData newsDetail(HttpServletRequest request){

        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long newsId = baseRequest.getLong("newsId");
        if(newsId == -1){
            return ResponseData.error("参数有误");
        }
        QueryWrapper<FlowNews> flowNewsQueryWrapper = new QueryWrapper<>();
        flowNewsQueryWrapper.eq("news_id",newsId);
        FlowNews flowNews = flowNewsService.getOne(flowNewsQueryWrapper);
        if(flowNews == null){
            return ResponseData.error("不存在该记录");
        }
        //图片地址替换
        flowNews.setCover(AppUtil.formatUrl(flowNews.getCover()));
        flowNews.setDetail(flowNews.getDetail().replace("\"/ueditor/","\""+AppUtil.domains+"/ueditor/"));
        return ResponseData.success(flowNews);
    }
    //查询常见问题
    //查询资讯中心
    @RequestMapping("/problemNews")
    @ResponseBody
    public ResponseData problemNews(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        int pagesize = baseRequest.getInt("pagesize",10);
        int cpage = baseRequest.getInt("cpage",1);
        long newsType = baseRequest.getLong("newsType");
        //
        QueryWrapper<FlowNewsProblem> flowNewsProblemQueryWrapper = new QueryWrapper<>();
        if(newsType != -1){
            flowNewsProblemQueryWrapper.eq("news_type_id",newsType);
        }
        flowNewsProblemQueryWrapper.orderByDesc("create_time");

        IPage page = new Page<FlowNewsProblem>(cpage,pagesize);
        page = flowNewsProblemService.getBaseMapper().selectPage(page,flowNewsProblemQueryWrapper);
        //
        List<FlowNewsProblem> list = page.getRecords();
        for (int i = 0;i<list.size();i++){
            list.get(i).setDetail(list.get(i).getDetail().replace("\"/ueditor/","\""+AppUtil.domains+"/ueditor/"));
        }
        //图片转换
        return ResponseData.success(list);
    }

    @RequestMapping("/problemNewsDetail")
    @ResponseBody
    public ResponseData problemNewsDetail(HttpServletRequest request){

        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long newsProblemId = baseRequest.getLong("newsProblemId");
        if(newsProblemId == -1){
            return ResponseData.error("参数有误");
        }
        QueryWrapper<FlowNewsProblem> flowNewsQueryWrapper = new QueryWrapper<>();
        flowNewsQueryWrapper.eq("news_problem_id",newsProblemId);
        FlowNewsProblem flowNewsProblem = flowNewsProblemService.getOne(flowNewsQueryWrapper);
        if(flowNewsProblem == null){
            return ResponseData.error("不存在该记录");
        }
        //图片地址替换
        flowNewsProblem.setDetail(flowNewsProblem.getDetail().replace("\"/ueditor/","\""+AppUtil.domains+"/ueditor/"));
        //查询类型
        QueryWrapper<FlowNewsType> flowNewsTypeQueryWrapper = new QueryWrapper<>();
        flowNewsTypeQueryWrapper.eq("news_type_id",flowNewsProblem.getNewsTypeId());
        FlowNewsType flowNewsType = flowNewsTypeService.getOne(flowNewsTypeQueryWrapper);
        if(flowNewsType != null){
            flowNewsProblem.setNewsTypeName(flowNewsType.getNewsTypeName());
        }
        return ResponseData.success(flowNewsProblem);
    }

    //查询服务案例
    @RequestMapping("/caseNews")
    @ResponseBody
    public ResponseData caseNews(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        int pagesize = baseRequest.getInt("pagesize",10);
        int cpage = baseRequest.getInt("cpage",1);
        long newsType = baseRequest.getLong("newsType");
        //
        QueryWrapper<FlowNewsCase> flowNewsCaseQueryWrapper = new QueryWrapper<>();
        if(newsType != -1){
            flowNewsCaseQueryWrapper.eq("news_type_id",newsType);
        }
        flowNewsCaseQueryWrapper.orderByDesc("create_time");

        IPage page = new Page<FlowNewsCase>(cpage,pagesize);
        page = flowNewsCaseService.getBaseMapper().selectPage(page,flowNewsCaseQueryWrapper);
        //
        List<FlowNewsCase> list = page.getRecords();
        for (int i = 0;i<list.size();i++){
            list.get(i).setCover(AppUtil.formatUrl(list.get(i).getCover()));
            list.get(i).setDetail(list.get(i).getDetail().replace("\"/ueditor/","\""+AppUtil.domains+"/ueditor/"));
            //替换姓名
            if(list.get(i).getServiceName() != null && list.get(i).getServiceName().length() > 1){
                list.get(i).setServiceName(list.get(i).getServiceName().substring(0,1) + "***");
            }
            if(list.get(i).getOrderCode() != null && list.get(i).getOrderCode().length() > 7){
                list.get(i).setOrderCode(list.get(i).getOrderCode().replaceAll("(.{3}).*(.{3})","$1****$2"));
            }
        }
        //图片转换
        return ResponseData.success(list);
    }

    @RequestMapping("/caseNewsDetail")
    @ResponseBody
    public ResponseData caseNewsDetail(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long newsCaseId = baseRequest.getLong("newsCaseId");
        if(newsCaseId == -1){
            return ResponseData.error("参数有误");
        }
        QueryWrapper<FlowNewsCase> flowNewsCaseQueryWrapper = new QueryWrapper<>();
        flowNewsCaseQueryWrapper.eq("news_case_id",newsCaseId);
        FlowNewsCase flowNewsCase = flowNewsCaseService.getOne(flowNewsCaseQueryWrapper);
        if(flowNewsCase == null){
            return ResponseData.error("不存在该记录");
        }
        //图片地址替换
        flowNewsCase.setCover(AppUtil.formatUrl(flowNewsCase.getCover()));
        flowNewsCase.setDetail(flowNewsCase.getDetail().replace("\"/ueditor/","\""+AppUtil.domains+"/ueditor/"));
        if(flowNewsCase.getServiceName() != null && flowNewsCase.getServiceName().length() > 1){
            flowNewsCase.setServiceName(flowNewsCase.getServiceName().substring(0,1) + "***");
        }
        if(flowNewsCase.getOrderCode() != null && flowNewsCase.getOrderCode().length() > 7){
            flowNewsCase.setOrderCode(flowNewsCase.getOrderCode().replaceAll("(.{3}).*(.{3})","$1****$2"));
        }
        //查询类型
        QueryWrapper<FlowNewsType> flowNewsTypeQueryWrapper = new QueryWrapper<>();
        flowNewsTypeQueryWrapper.eq("news_type_id",flowNewsCase.getNewsTypeId());
        FlowNewsType flowNewsType = flowNewsTypeService.getOne(flowNewsTypeQueryWrapper);
        if(flowNewsType != null){
            flowNewsCase.setNewsTypeName(flowNewsType.getNewsTypeName());
        }
        return ResponseData.success(flowNewsCase);
    }

    //查询用户指南
    @RequestMapping("/flowUserguide")
    @ResponseBody
    public ResponseData flowUserguide(){
        //查询类型
        QueryWrapper<FlowUserguideType> flowUserguideTypeQueryWrapper = new QueryWrapper<>();
        flowUserguideTypeQueryWrapper.orderByAsc("orders");
        List<FlowUserguideType> flowUserguideTypes = flowUserguideTypeService.list(flowUserguideTypeQueryWrapper);
        //查询所有的帮助
        QueryWrapper<FlowUserguide> flowUserguideQueryWrapper = new QueryWrapper<>();
        flowUserguideQueryWrapper.orderByAsc("orders");
        List<FlowUserguide> flowUserguideList = flowUserguideService.list(flowUserguideQueryWrapper);
        //根据类型排序，再获取明细排序，再查询问题
        //搬家前，搬家中，搬家后
        for (int i = 0;i<flowUserguideTypes.size();i++){
            List<FlowUserguide> flowUserguides =  new ArrayList<>();
            for (int j = 0;j<flowUserguideList.size();j++){
                if(flowUserguideTypes.get(i).getUserguideTypeId().longValue() == flowUserguideList.get(j).getUserguideTypeId().longValue()){
                    flowUserguides.add(flowUserguideList.get(j));
                }
            }
            flowUserguideTypes.get(i).setFlowUserguides(flowUserguides);
        }

        return ResponseData.success(flowUserguideTypes);
    }

    //查询关于我们
    @RequestMapping("/about")
    @ResponseBody
    public ResponseData about(){
        QueryWrapper<FlowAbout> flowAboutQueryWrapper = new QueryWrapper<>();
        flowAboutQueryWrapper.eq("about_id",1);
        FlowAbout flowAbout = flowAboutService.getOne(flowAboutQueryWrapper);
        flowAbout.setAdCover(AppUtil.formatUrl(flowAbout.getAdCover()));
        flowAbout.setBanner(AppUtil.formatUrl(flowAbout.getBanner()));
        return ResponseData.success(flowAbout);
    }

    //查询全国网点
    @RequestMapping("/storeCitys")
    @ResponseBody
    public ResponseData storeCitys(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        int pagesize = baseRequest.getInt("pagesize",10);
        int cpage = baseRequest.getInt("cpage",1);
        String storeName = baseRequest.getString("storeName");
        QueryWrapper<FlowStoreCity> flowStoreCityQueryWrapper = new QueryWrapper<>();
        flowStoreCityQueryWrapper.like("store_name",storeName);
        flowStoreCityQueryWrapper.orderByDesc("is_detault");
        IPage page = new Page<FlowStoreCity>(cpage,pagesize);
        page = flowStoreCityService.getBaseMapper().selectPage(page,flowStoreCityQueryWrapper);
        List<FlowStoreCity> flowStoreCities = page.getRecords();
        //查询备注
        if(flowStoreCities.size() != 0){
            List<Long> ids = new ArrayList<>();
            for (int i = 0;i<flowStoreCities.size();i++){
                ids.add(flowStoreCities.get(i).getStoreCityId());
            }
            QueryWrapper<FlowStoreCityNote> flowStoreCityNoteQueryWrapper = new QueryWrapper<>();
            flowStoreCityNoteQueryWrapper.in("store_city_id",ids);
            flowStoreCityNoteQueryWrapper.orderByAsc("orders");
            List<FlowStoreCityNote> flowStoreCityNotes = flowStoreCityNoteService.list(flowStoreCityNoteQueryWrapper);
            for (int i = 0;i<flowStoreCities.size();i++){
                List<FlowStoreCityNote> storeCityNotes = new ArrayList<>();
                for (int j = 0;j<flowStoreCityNotes.size();j++){
                    if(flowStoreCities.get(i).getStoreCityId().longValue() == flowStoreCityNotes.get(j).getStoreCityId().longValue()){
                        storeCityNotes.add(flowStoreCityNotes.get(j));
                    }
                }

                flowStoreCities.get(i).setFlowStoreCityNotes(storeCityNotes);
            }
        }

        return ResponseData.success(flowStoreCities);
    }

    //查询平台配置
    @RequestMapping("/flowConfig")
    @ResponseBody
    public ResponseData flowConfig(){
        QueryWrapper<FlowConfig> flowConfigQueryWrapper = new QueryWrapper<>();
        flowConfigQueryWrapper.eq("config_id",1);
        FlowConfig flowConfig = flowConfigService.getOne(flowConfigQueryWrapper);
        //查询评论人数
        QueryWrapper<FlowMemberComment> flowMemberCommentQueryWrapper = new QueryWrapper<>();
        long count = flowMemberCommentService.count(flowMemberCommentQueryWrapper);
        String comment = "";
        if(count < 1000){
            comment = count + "";
        }else if(count < 10000){
            comment = count / 1000 +"千";
        }else if(count < 20000){
            comment = count / 10000 +"万";
        }else {
            comment = "2万+";
        }
        flowConfig.setCommmentCount(comment);
        flowConfig.setCouponTop(AppUtil.formatUrl(flowConfig.getCouponTop()));
        flowConfig.setCouponBottom(AppUtil.formatUrl(flowConfig.getCouponBottom()));
        flowConfig.setPlatformCover(AppUtil.formatUrl(flowConfig.getPlatformCover()));
        flowConfig.setQcodeSlogan(flowConfig.getQcodeSlogan());
        return ResponseData.success(flowConfig);
    }

    //查询用户评论
    @RequestMapping("/memberComments")
    @ResponseBody
    public ResponseData memberComments(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        int pagesize = baseRequest.getInt("pagesize",10);
        int cpage = baseRequest.getInt("cpage",1);
        int type = baseRequest.getInt("type",0);
        QueryWrapper<FlowMemberComment> flowMemberCommentQueryWrapper = new QueryWrapper<>();
        if(type == 1){
            flowMemberCommentQueryWrapper.apply("img_urls != ''");
        }
        flowMemberCommentQueryWrapper.orderByDesc("comment_id");
        IPage page = new Page<FlowMemberComment>(cpage,pagesize);
        page = flowMemberCommentService.getBaseMapper().selectPage(page,flowMemberCommentQueryWrapper);
        List<FlowMemberComment> flowMemberComments = page.getRecords();
        //图片
        for (int i = 0;i<flowMemberComments.size();i++){
            String imgUrls = flowMemberComments.get(i).getImgUrls();
            if(imgUrls != null && !imgUrls.equals("")){
                String arr[] = imgUrls.split(",");
                for (int j = 0;j<arr.length;j++){
                    arr[j] = AppUtil.formatUrl(arr[j]);
                }
                String nImgUrls = "";
                for (int j = 0;j<arr.length;j++){
                    if(j == 0){
                        nImgUrls += arr[j];
                    }else {
                        nImgUrls += ","+arr[j];
                    }
                }
                flowMemberComments.get(i).setImgUrls(nImgUrls);
            }

            if(flowMemberComments.get(i).getMemberName() != null && flowMemberComments.get(i).getMemberName().length() > 1){
                flowMemberComments.get(i).setMemberName(flowMemberComments.get(i).getMemberName().substring(0,1) + "***");
            }
            if(flowMemberComments.get(i).getMemberPhone() != null && flowMemberComments.get(i).getMemberPhone().length() > 7){
                flowMemberComments.get(i).setMemberPhone(flowMemberComments.get(i).getMemberPhone().replaceAll("(.{3}).*(.{3})","$1****$2"));
            }
        }

        return ResponseData.success(flowMemberComments);
    }

    //根据类型返回查询的资料
    @RequestMapping("/service")
    @ResponseBody
    public ResponseData service(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long id = baseRequest.getLong("id");
        if(id == -1){
            return ResponseData.error("请选择服务类型");
        }
        QueryWrapper<FlowService> flowServiceQueryWrapper = new QueryWrapper<>();
        flowServiceQueryWrapper.eq("service_id",id);
        FlowService flowService = flowServiceService.getOne(flowServiceQueryWrapper);
        if(flowService == null){
            return ResponseData.error("不存在该数据");
        }
        flowService.setBgUrl(AppUtil.formatUrl(flowService.getBgUrl()));
        //根据类型查询服务资料
        return ResponseData.success(flowService);
    }

    //查询同城的车辆类型
    @RequestMapping("/serviceType")
    @ResponseBody
    public ResponseData serviceType(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        int module = baseRequest.getInt("module",0);
        QueryWrapper<FlowServiceType> flowServiceTypeQueryWrapper = new QueryWrapper<>();
        flowServiceTypeQueryWrapper.eq("module",module);
        flowServiceTypeQueryWrapper.eq("status",0);
        flowServiceTypeQueryWrapper.orderByAsc("orders");
        List<FlowServiceType> flowServiceTypes = flowServiceTypeService.list(flowServiceTypeQueryWrapper);
        //图片转换
        for (int i = 0;i<flowServiceTypes.size();i++){
            flowServiceTypes.get(i).setTypeUrl(AppUtil.formatUrl(flowServiceTypes.get(i).getTypeUrl()));
        }

        return ResponseData.success(flowServiceTypes);
    }
    //查询服务详情页
    @RequestMapping("/serviceTypeV1")
    @ResponseBody
    public ResponseData serviceTypeV1(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String city = baseRequest.getString("city");
        String province = baseRequest.getString("province");
        FlowStoreCity flowStoreCity;
        if(city.equals("") && province.equals("")){
            //查询默认
            QueryWrapper<FlowStoreCity> flowStoreCityQueryWrapper = new QueryWrapper<>();
            flowStoreCityQueryWrapper.eq("is_detault",1);
            flowStoreCityQueryWrapper.last("limit 1");
            flowStoreCity = flowStoreCityService.getOne(flowStoreCityQueryWrapper);
        }else {
            QueryWrapper<FlowStoreCity> flowStoreCityQueryWrapper = new QueryWrapper<>();
            flowStoreCityQueryWrapper.eq("province",province);
            if(!city.equals("")){
                flowStoreCityQueryWrapper.eq("city",city);
            }
            flowStoreCityQueryWrapper.last("limit 1");
            flowStoreCity = flowStoreCityService.getOne(flowStoreCityQueryWrapper);
        }
        if(flowStoreCity == null){
            return ResponseData.success("该城市没有网点");
        }
        //查询服务点车辆
        int module = baseRequest.getInt("module",0);
        QueryWrapper<FlowServiceType> flowServiceTypeQueryWrapper = new QueryWrapper<>();
        flowServiceTypeQueryWrapper.eq("module",module);
        flowServiceTypeQueryWrapper.eq("status",0);
        flowServiceTypeQueryWrapper.apply("service_type_id in (select service_type_id from flow_service_type_price " +
                "where store_city_id = "+flowStoreCity.getStoreCityId().longValue()+" and car_price >= 0)");
        flowServiceTypeQueryWrapper.orderByAsc("orders");
        List<FlowServiceType> flowServiceTypes = flowServiceTypeService.list(flowServiceTypeQueryWrapper);
        //图片转换
        for (int i = 0;i<flowServiceTypes.size();i++){
            flowServiceTypes.get(i).setTypeUrl(AppUtil.formatUrl(flowServiceTypes.get(i).getTypeUrl()));
        }

        return ResponseData.success(flowServiceTypes);
    }

    //用户登录接口
    @RequestMapping("/zfbLogin")
    @ResponseBody
    public ResponseData zfbLogin(HttpServletRequest request) throws Exception {
        //修改成获取手机号登录
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String authCode = baseRequest.getString("authCode");
        if(authCode.equals("")){
            return ResponseData.error("请先授权登录");
        }

        AlipayClient alipayClient = new AliUtil().getAlipayClient();
        AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
        alipaySystemOauthTokenRequest.setGrantType("authorization_code");
        alipaySystemOauthTokenRequest.setCode(authCode);

        AlipaySystemOauthTokenResponse alipaySystemOauthTokenResponse = alipayClient.certificateExecute(alipaySystemOauthTokenRequest);
        if(alipaySystemOauthTokenResponse == null || !alipaySystemOauthTokenResponse.isSuccess()){
            return ResponseData.error(alipaySystemOauthTokenResponse.getMsg());
        }
        String nickName = baseRequest.getString("nickName");
        String faceUrl = baseRequest.getString("faceUrl");
        String encryptedData = baseRequest.getString("encryptedData");
        //如果encryptedData不为空，获取手机号
        String mobile = "";
        if(!encryptedData.equals("")){
            try{
                String pain = AliUtil.AliConfig.AESDecrypt(encryptedData);
                Map<String,String> res = JSON.parseObject(pain,Map.class);
                if(res.containsKey("mobile")){
                    mobile = res.get("mobile");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //成功，返回openid
        //查询数据库是否存在该用户
        QueryWrapper<FlowZfbMember> flowZfbMemberQueryWrapper = new QueryWrapper<>();
        flowZfbMemberQueryWrapper.eq("zfb_openid",alipaySystemOauthTokenResponse.getUserId());
        FlowZfbMember flowZfbMember = flowZfbMemberService.getOne(flowZfbMemberQueryWrapper);
        if(flowZfbMember == null){
            //添加
            flowZfbMember = new FlowZfbMember();
            flowZfbMember.setIsNew(0);
            flowZfbMember.setCreateTime(new Date());
            flowZfbMember.setFaceUrl(faceUrl);
            flowZfbMember.setNickName(nickName);
            flowZfbMember.setAccount(mobile);
            flowZfbMember.setUpdateTime(new Date());
            flowZfbMember.setZfbOpenid(alipaySystemOauthTokenResponse.getUserId());
            boolean result = flowZfbMemberService.save(flowZfbMember);
            //发送新用户通知短信
            if(StringUtils.isEmpty(nickName)){
                nickName="xxx";
            }
            this.sendRefundMsg(nickName,mobile);
            if(result){
                if(!mobile.equals("")) {
                    //新人，领取
                    //领取卷
                    QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                    flowMemberCouponActivityQueryWrapper.eq("status", 0);
                    List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                    if (couponActivities.size() != 0) {
                        //查询该账户的历史领取
                        List<Long> ids = new ArrayList<>();

                        for (int i = 0; i < couponActivities.size(); i++) {
                            ids.add(couponActivities.get(i).getCouponActivityId());
                        }

                        QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                        flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                        flowMemberCouponActivityRecordQueryWrapper.eq("type",0);
                        flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowZfbMember.getMemberId());
                        List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                        for (int i = 0; i < couponActivities.size(); i++) {
                            boolean get = false;
                            for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                                if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                    get = true;
                                }
                            }
                            if (!get) {
                                //领取
                                FlowCoupon flowCoupon = new FlowCoupon();
                                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                                flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                                flowCoupon.setCreateTime(new Date());
                                flowCoupon.setIsGet(1);
                                flowCoupon.setType(0);
                                flowCoupon.setMemberId(flowZfbMember.getMemberId());
                                flowCoupon.setUpdateTime(new Date());
                                flowCoupon.setGetTime(new Date());
                                flowCoupon.setIsUsed(0);
                                flowCoupon.setUseTime(new Date());
                                flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                                flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                                flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                                if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                    flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                                } else {
                                    flowCoupon.setEndTime(new Date());
                                }
                                flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                                flowCouponService.save(flowCoupon);
                                //领取记录
                                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                                flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                                flowMemberCouponActivityRecord.setCreateTime(new Date());
                                flowMemberCouponActivityRecord.setMemberId(flowZfbMember.getMemberId());
                                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                                flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                            }
                        }
                    }

                    FlowZfbMemberParam flowZfbMemberParam = new FlowZfbMemberParam();
                    flowZfbMemberParam.setIsNew(1);
                    flowZfbMemberParam.setMemberId(flowZfbMember.getMemberId());
                    flowZfbMemberService.update(flowZfbMemberParam);
                }
                //
                return ResponseData.success(flowZfbMember.getZfbOpenid());
            }else {
                return ResponseData.error("登录失败");
            }
        }else{
            if(!mobile.equals("")){
                flowZfbMember.setAccount(mobile);
            }
            flowZfbMember.setNickName(nickName);
            flowZfbMember.setFaceUrl(faceUrl);
            flowZfbMemberService.updateById(flowZfbMember);
            //查询是否已经绑定过
            if(!mobile.equals("") && flowZfbMember.getIsNew().intValue() == 0) {
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                flowMemberCouponActivityQueryWrapper.eq("status", 0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if (couponActivities.size() != 0) {
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0; i < couponActivities.size(); i++) {
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type", 0);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowZfbMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0; i < couponActivities.size(); i++) {
                        boolean get = false;
                        for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                            if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                get = true;
                            }
                        }
                        if (!get) {
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowZfbMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setType(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                            } else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowZfbMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束
                FlowZfbMemberParam flowZfbMemberParam = new FlowZfbMemberParam();
                flowZfbMemberParam.setIsNew(1);
                flowZfbMemberParam.setMemberId(flowZfbMember.getMemberId());
                flowZfbMemberService.update(flowZfbMemberParam);
            }
            //
            return ResponseData.success(flowZfbMember.getZfbOpenid());
        }
    }

    @RequestMapping("/phoneLogin")
    @ResponseBody
    public ResponseData phoneLogin(HttpServletRequest request) throws AlipayApiException {
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String phone = baseRequest.getString("phone");
        String authCode = baseRequest.getString("authCode");
        String code = baseRequest.getString("code");
        if(code.equals("")){
            return ResponseData.error("请输入手机验证码");
        }

        //查询手机验证码
        QueryWrapper<FlowPhoneCode> flowPhoneCodeQueryWrapper = new QueryWrapper<>();
        flowPhoneCodeQueryWrapper.eq("phone",phone);
        flowPhoneCodeQueryWrapper.apply("create_time >= DATE_SUB(NOW(),INTERVAL 5 MINUTE)");
        flowPhoneCodeQueryWrapper.orderByDesc("create_time");
        flowPhoneCodeQueryWrapper.last("limit 1");
        FlowPhoneCode flowPhoneCode = flowPhoneCodeService.getOne(flowPhoneCodeQueryWrapper);
        if(flowPhoneCode == null){
            return ResponseData.error("请重新获取验证码");
        }
        if(!code.equals(flowPhoneCode.getCode())){
            return ResponseData.error("验证码错误");
        }

        AlipayClient alipayClient = new AliUtil().getAlipayClient();
        AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
        alipaySystemOauthTokenRequest.setGrantType("authorization_code");
        alipaySystemOauthTokenRequest.setCode(authCode);

        AlipaySystemOauthTokenResponse alipaySystemOauthTokenResponse = alipayClient.certificateExecute(alipaySystemOauthTokenRequest);
        if(alipaySystemOauthTokenResponse == null || !alipaySystemOauthTokenResponse.isSuccess()){
            return ResponseData.error(alipaySystemOauthTokenResponse.getMsg());
        }

        //查询
        QueryWrapper<FlowZfbMember> flowZfbMemberQueryWrapper = new QueryWrapper<>();
        flowZfbMemberQueryWrapper.eq("zfb_openid",alipaySystemOauthTokenResponse.getUserId());
        FlowZfbMember flowZfbMember = flowZfbMemberService.getOne(flowZfbMemberQueryWrapper);
        if(flowZfbMember == null){
            flowZfbMember = new FlowZfbMember();
            flowZfbMember.setAccount(phone);
            flowZfbMember.setCreateTime(new Date());
            flowZfbMember.setFaceUrl("");
            flowZfbMember.setNickName("");
            flowZfbMember.setUpdateTime(new Date());
            flowZfbMember.setIsNew(1);
            flowZfbMember.setZfbOpenid(alipaySystemOauthTokenResponse.getUserId());
            boolean result = flowZfbMemberService.save(flowZfbMember);
            if(result){
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
                flowMemberCouponActivityQueryWrapper.eq("status",0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if(couponActivities.size() != 0){
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0;i<couponActivities.size();i++){
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id",ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",0);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowZfbMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0;i<couponActivities.size();i++){
                        boolean get = false;
                        for (int j = 0;j<flowMemberCouponActivityRecords.size();j++){
                            if(couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()){
                                get = true;
                            }
                        }
                        if(!get){
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowZfbMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setType(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if(couponActivities.get(i).getOutDate().intValue() > 0){
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(),couponActivities.get(i).getOutDate().intValue()));
                            }else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowZfbMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }

                //结束
                return ResponseData.success(flowZfbMember.getZfbOpenid());
            } else {
                return ResponseData.error("登录失败");
            }
        }else {
            flowZfbMember.setAccount(phone);
            flowZfbMemberService.updateById(flowZfbMember);
            if(flowZfbMember.getIsNew().intValue() == 0){
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
                flowMemberCouponActivityQueryWrapper.eq("status",0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if(couponActivities.size() != 0){
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0;i<couponActivities.size();i++){
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id",ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",0);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowZfbMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0;i<couponActivities.size();i++){
                        boolean get = false;
                        for (int j = 0;j<flowMemberCouponActivityRecords.size();j++){
                            if(couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()){
                                get = true;
                            }
                        }
                        if(!get){
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowZfbMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setType(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if(couponActivities.get(i).getOutDate().intValue() > 0){
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(),couponActivities.get(i).getOutDate().intValue()));
                            }else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowZfbMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }

                //结束
                FlowZfbMemberParam flowZfbMemberParam = new FlowZfbMemberParam();
                flowZfbMemberParam.setIsNew(1);
                flowZfbMemberParam.setMemberId(flowZfbMember.getMemberId());
                flowZfbMemberService.update(flowZfbMemberParam);
            }
            return ResponseData.success(flowZfbMember.getZfbOpenid());
        }
    }

    @RequestMapping("/phoneCode")
    @ResponseBody
    public ResponseData phoneCode(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String timestamp = request.getHeader("timestamp");
        String code = request.getHeader("code");
        String phone = baseRequest.getString("phone");

        if(phone.equals("")){
            return ResponseData.error("请输入手机号");
        }
        if(code == null){
            return ResponseData.error("参数有误01");
        }
        if(timestamp == null){
            return ResponseData.error("参数有误02");
        }
        if(code == null){
            return ResponseData.error("参数有误03");
        }
        long time = Long.parseLong(timestamp);
        Long s = (System.currentTimeMillis() - time) / (1000 * 60);
        if(s > 1){
            return ResponseData.error("登录时间已过期，请重新登录.");
        }
        String ncode = MD5Util.encrypt(phone + AppUtil.key + timestamp);
        if(!ncode.toLowerCase().equals(code.toLowerCase())){
            return ResponseData.error("校验失败，请重新尝试.");
        }

        if(phone.length() != 11){
            return ResponseData.error("手机号格式错误");
        }

        //查询是否在1分钟内发送过验证码
        QueryWrapper<FlowPhoneCode> flowPhoneCodeQueryWrapper = new QueryWrapper<>();
        flowPhoneCodeQueryWrapper.eq("phone",phone);
        flowPhoneCodeQueryWrapper.apply("create_time >= DATE_SUB(NOW(),INTERVAL 1 MINUTE)");
        int count = flowPhoneCodeService.count(flowPhoneCodeQueryWrapper);
        if(count == 0){
            //可以发送
            FlowPhoneCode flowPhoneCode = new FlowPhoneCode();
            flowPhoneCode.setCode(CommonUtil.phoneCode());
            flowPhoneCode.setCreateTime(new Date());
            flowPhoneCode.setPhone(phone);
            flowPhoneCode.setType(0);
            boolean send = AliyuncsUtil.sendSms(0,phone,flowPhoneCode.getCode());
            boolean result = flowPhoneCodeService.save(flowPhoneCode);
            if(result){
                return ResponseData.success("验证码发送成功");
            }else {
                return ResponseData.error("验证码发送失败");
            }
        }else {
            return ResponseData.error("验证码已发送，请等1分钟后再试~");
        }
    }

    //查询问题反馈类型
    @RequestMapping("/problemType")
    @ResponseBody
    public ResponseData problemType(){
        QueryWrapper<FlowProblemType> flowProblemTypeQueryWrapper = new QueryWrapper<>();
        flowProblemTypeQueryWrapper.orderByAsc("orders");
        return ResponseData.success(flowProblemTypeService.list(flowProblemTypeQueryWrapper));
    }

    @RequestMapping("/couponActivitys")
    @ResponseBody
    public ResponseData couponActivitys(){
        QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
        flowMemberCouponActivityQueryWrapper.eq("status",0);
        flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
        List<FlowMemberCouponActivity> flowMemberCouponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
        List<FlowService> flowServices = flowServiceService.list();
        for (int i = 0;i<flowMemberCouponActivities.size();i++){
            for (int j = 0;j<flowServices.size();j++){
                if(flowMemberCouponActivities.get(i).getServiceType().longValue() == flowServices.get(j).getServiceId().longValue()){
                    flowMemberCouponActivities.get(i).setServiceName(flowServices.get(j).getServiceName());
                }
            }
        }
        return ResponseData.success(flowMemberCouponActivities);
    }


    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public ResponseData updateUser(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String userId = baseRequest.getString("userId");
        String supUserId = baseRequest.getString("supUserId");

        QueryWrapper<FlowWxMember> flowWxMemberQueryWrapper = new QueryWrapper<>();
        flowWxMemberQueryWrapper.eq("member_id",userId);
        FlowWxMember flowWxMember = flowWxMemberService.getOne(flowWxMemberQueryWrapper);
        if(StringUtils.isEmpty(flowWxMember.getSuperUserId())){
            flowWxMember.setSuperUserId(Long.valueOf(supUserId));
            flowWxMemberService.updateById(flowWxMember);
        }


        return ResponseData.success(flowWxMember.getSuperUserId());
    }


    //高德地图根据坐标得到省市
    @RequestMapping("/geocode")
    @ResponseBody
    public ResponseData geocode(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String location = baseRequest.getString("location");
        if(location.equals("")){
            return ResponseData.error("位置获取失败");
        }

        return ResponseData.success(GaoDeMapUtil.Geocode(location));
    }

    //百度地图poi
    @RequestMapping("/bdGeocode")
    @ResponseBody
    public ResponseData bdGeocode(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String location = baseRequest.getString("location");
        if(location.equals("")){
            return ResponseData.error("位置获取失败");
        }
        String []arr = location.split(",");
        location = arr[1] + "," + arr[0];
        return ResponseData.success(BaiduMapUtil.Geocode(location));
    }

    //腾讯地图api
    @RequestMapping("/wxGeocode")
    @ResponseBody
    public ResponseData wxGeocode(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String location = baseRequest.getString("location");
        if(location.equals("")){
            return ResponseData.error("位置获取失败");
        }
        String []arr = location.split(",");
        location = arr[1] + "," + arr[0];
        return ResponseData.success(TencentMapUtil.Geocode(location));
    }

    @RequestMapping("/allCity")
    @ResponseBody
    public ResponseData allCity(){
        QueryWrapper<FlowCity> flowCityQueryWrapper = new QueryWrapper<>();
        flowCityQueryWrapper.eq("hide",0);
        flowCityQueryWrapper.apply("province in (select province from flow_province where hide = 0)");
        return ResponseData.success(flowCityService.list(flowCityQueryWrapper));
    }

    //查询楼层
    @RequestMapping("/floorLevel")
    @ResponseBody
    public ResponseData floorLevel(){
        QueryWrapper<FlowFloorLevel> flowFloorLevelQueryWrapper = new QueryWrapper<>();
        flowFloorLevelQueryWrapper.orderByAsc("orders");
        return ResponseData.success(flowFloorLevelService.list(flowFloorLevelQueryWrapper));
    }

    @RequestMapping("/storageDate")
    @ResponseBody
    public ResponseData storageDate(){
        QueryWrapper<FlowStorageDate> flowStorageDateQueryWrapper = new QueryWrapper<>();
        flowStorageDateQueryWrapper.orderByAsc("orders");
        return ResponseData.success(flowStorageDateService.list(flowStorageDateQueryWrapper));
    }
    //查询仓储日期
    //国家和城市数据
    @RequestMapping("/allCountry")
    @ResponseBody
    public ResponseData allCountry(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long countryId = baseRequest.getLong("countryId");
        if(countryId == -1){
            //查询
            QueryWrapper<FlowCountry> flowCountryQueryWrapper = new QueryWrapper<>();
            flowCountryQueryWrapper.eq("hide",0);
            flowCountryQueryWrapper.orderByAsc("count_abbr");
            List<FlowCountry> flowCountries = flowCountryService.list(flowCountryQueryWrapper);
            return ResponseData.success(flowCountries);
        }else {
            QueryWrapper<FlowCountryCity> flowCountryCityQueryWrapper = new QueryWrapper<>();
            flowCountryCityQueryWrapper.eq("hide",0);
            flowCountryCityQueryWrapper.eq("country_id",countryId);
            flowCountryCityQueryWrapper.orderByAsc("city_addr");
            List<FlowCountryCity> flowCountryCities = flowCountryCityService.list(flowCountryCityQueryWrapper);
            return ResponseData.success(flowCountryCities);
        }
    }

    //根据城市位置仓储点位置
    //如果没有，不让提交
    //查询
    @RequestMapping("/queryStore")
    @ResponseBody
    public ResponseData queryStore(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String province = baseRequest.getString("province");
        String city = baseRequest.getString("city");
        int type = baseRequest.getInt("type");
        if(province.equals("")){
            return ResponseData.error("请选择城市");
        }

        QueryWrapper<FlowStoreCity> flowStoreCityQueryWrapper = new QueryWrapper<>();
        if(!province.equals("")){
            flowStoreCityQueryWrapper.eq("province",province);
        }

        if(!city.equals("")){
            flowStoreCityQueryWrapper.eq("city",city);
        }
        flowStoreCityQueryWrapper.last("limit 1");
        FlowStoreCity flowStoreCity = flowStoreCityService.getOne(flowStoreCityQueryWrapper);
        if(flowStoreCity == null){
            if(type == 1){
                //查询默认网点
                flowStoreCityQueryWrapper = new QueryWrapper<>();
                flowStoreCityQueryWrapper.eq("is_detault",1);
                flowStoreCityQueryWrapper.last("limit 1");
                flowStoreCity = flowStoreCityService.getOne(flowStoreCityQueryWrapper);
                if(flowStoreCity != null){
                    return ResponseData.success(flowStoreCity);
                }
            }
            return ResponseData.success("啊哦，您所在的位置没有服务点，请重新选择~");
        }
        return ResponseData.success(flowStoreCity);
    }
    //查询服务类型
    @RequestMapping("/queryServiceType")
    @ResponseBody
    public ResponseData queryServiceType(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long id = baseRequest.getLong("id");
        long storeId = baseRequest.getLong("storeId");
        if(id == -1){
            return ResponseData.error("请选择车辆类型");
        }
        if(storeId == -1){
            return ResponseData.error("定位失败");
        }
        //查询汽车类型
        QueryWrapper<FlowServiceType> flowServiceTypeQueryWrapper = new QueryWrapper<>();
        flowServiceTypeQueryWrapper.eq("service_type_id",id);
        flowServiceTypeQueryWrapper.eq("status",0);
        FlowServiceType flowServiceType = flowServiceTypeService.getOne(flowServiceTypeQueryWrapper);
        if(flowServiceType == null){
            return ResponseData.error("不存在该类型");
        }
        //查询价格
        QueryWrapper<FlowServiceTypePrice> flowServiceTypePriceQueryWrapper = new QueryWrapper<>();
        flowServiceTypePriceQueryWrapper.eq("service_type_id",flowServiceType.getServiceTypeId());
        flowServiceTypePriceQueryWrapper.eq("store_city_id",storeId);
        flowServiceTypePriceQueryWrapper.last("limit 1");
        double price = 0;
        FlowServiceTypePrice flowServiceTypePrice = flowServiceTypePriceService.getOne(flowServiceTypePriceQueryWrapper);
        if(flowServiceTypePrice != null){
            price = flowServiceTypePrice.getCarPrice().doubleValue();
        }
        flowServiceType.setPrice(new BigDecimal(price));
        return ResponseData.success(flowServiceType);
    }

    //查询楼层费用
    @RequestMapping("/elevatorPrice")
    @ResponseBody
    public ResponseData elevatorPrice(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String elevator = baseRequest.getString("elevator");
        Long storeId = baseRequest.getLong("storeId");
        Long serviceTypeId = baseRequest.getLong("serviceTypeId");
        QueryWrapper<FlowElevatorPrice> flowElevatorPriceQueryWrapper = new QueryWrapper<>();
        flowElevatorPriceQueryWrapper.eq("store_city_id",storeId);
        flowElevatorPriceQueryWrapper.eq("service_type_id",serviceTypeId);
        flowElevatorPriceQueryWrapper.apply("elevator_id in (select level_id from flow_floor_level where level_name = '"+elevator+"')");
        FlowElevatorPrice flowElevatorPrice = flowElevatorPriceService.getOne(flowElevatorPriceQueryWrapper);
        if(flowElevatorPrice == null){
            return ResponseData.success(0);
        }else {
            return ResponseData.success(flowElevatorPrice.getPrice().doubleValue());
        }
    }

    //获取所有的分类
    @RequestMapping("/goodsInfo")
    @ResponseBody
    public ResponseData goodsInfo(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long storeId = baseRequest.getLong("storeId");
        if(storeId == -1){
            return ResponseData.error("无法确认您的服务位置");
        }
        //查询类型，以及价格
        List<FlowGoodsType> flowGoodsTypes = flowGoodsTypeService.list();
        List<FlowGoodsType> result = new ArrayList<>();
        //查询物品以及价格
        FlowGoodsParam flowGoodsParam = new FlowGoodsParam();
        flowGoodsParam.setStoreCityId(storeId);
        List<FlowGoodsResult> flowGoodsResults = flowGoodsService.selectGoodsList(flowGoodsParam);

        for (int i = 0;i<flowGoodsTypes.size();i++){
            List<FlowGoodsResult> goodsResults = new ArrayList<>();

            for (int j = 0;j<flowGoodsResults.size();j++){
                if(flowGoodsTypes.get(i).getGoodsTypeId().longValue() == flowGoodsResults.get(j).getGoodsTypeId().longValue()){
                    if(flowGoodsResults.get(j).getCover() == null || flowGoodsResults.get(j).getCover().equals("")){
                        flowGoodsResults.get(j).setCover("/resource/images/icon-noimg.png");
                    }else {
                        flowGoodsResults.get(j).setCover(AppUtil.formatUrl(flowGoodsResults.get(j).getCover()));
                    }
                    if(flowGoodsResults.get(j).getGoodsPrice() != null && flowGoodsResults.get(j).getGoodsPrice().doubleValue() >= 0){
                        goodsResults.add(flowGoodsResults.get(j));
                    }
                }
            }

            flowGoodsTypes.get(i).setFlowGoodsResults(goodsResults);
            //大于0的显示
            if(goodsResults.size() > 0){
                result.add(flowGoodsTypes.get(i));
            }
        }

        return ResponseData.success(result);
    }

    //查询两位置的公里数
    @RequestMapping("/queryDistance")
    @ResponseBody
    public ResponseData queryDistance(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        long id = baseRequest.getLong("id");
        long storeId = baseRequest.getLong("storeId");
        if(id == -1){
            return ResponseData.error("请选择车辆类型");
        }
        if(storeId == -1){
            return ResponseData.error("定位失败");
        }
        String begLocation = baseRequest.getString("begLocation");
        String endLocation = baseRequest.getString("endLocation");
        if(begLocation.equals("")){
            return ResponseData.error("请选择开始位置");
        }
        if(endLocation.equals("")){
            return ResponseData.error("请选择终点位置");
        }
        //转换成百度坐标
        BaiduTransitionResult baiduTransitionResult = BaiduMapUtil.transition(begLocation);
        String bdBegLocation = "";
        String bdEndLocation = "";
        if(baiduTransitionResult != null && baiduTransitionResult.getStatus() == 0){
            bdBegLocation = baiduTransitionResult.getResult().get(0).getY() + ","+baiduTransitionResult.getResult().get(0).getX();
        }

        baiduTransitionResult = BaiduMapUtil.transition(endLocation);
        if(baiduTransitionResult != null && baiduTransitionResult.getStatus() == 0){
            bdEndLocation = baiduTransitionResult.getResult().get(0).getY() + ","+baiduTransitionResult.getResult().get(0).getX();
        }
        if(bdBegLocation.equals("")){
            return ResponseData.error("请选择开始位置");
        }
        if(bdEndLocation.equals("")){
            return ResponseData.error("请选择终点位置");
        }
        BaiduDistanceResult baiduDistanceResult = BaiduMapUtil.distance(bdBegLocation,bdEndLocation);

        //获取汽车的公里费用
        QueryWrapper<FlowServiceDistancePrice> flowServiceDistancePriceQueryWrapper = new QueryWrapper<>();
        flowServiceDistancePriceQueryWrapper.eq("service_type_id",id);
        flowServiceDistancePriceQueryWrapper.eq("store_city_id",storeId);
        flowServiceDistancePriceQueryWrapper.orderByAsc("distance_begin");
        List<FlowServiceDistancePrice> flowServiceTypePrices = flowServiceDistancePriceService.list(flowServiceDistancePriceQueryWrapper);
        List<FlowServiceDistancePrice> result = new ArrayList<>();
        double distancePrice = 0;
        //成功
        double distance =  new  BigDecimal(Double.parseDouble(baiduDistanceResult.getResult().get(0).getDistance().get("value").toString()) * 1.0/ 1000)
                .setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();;//单位km
        //如果两地相等，直接等于0
        if(begLocation.equals(endLocation)){
            distance = 0;
        }
        //根据距离计算
        if(flowServiceTypePrices.size() == 1){
            //按统一的价格计算
            if(flowServiceTypePrices.get(0).getDistanceEnd().doubleValue() <= distance){
                if(flowServiceTypePrices.get(0).getType().intValue() == 0){
                    distancePrice = distance * flowServiceTypePrices.get(0).getDistancePrice().doubleValue();
                    flowServiceTypePrices.get(0).setTotalPrice(distance * flowServiceTypePrices.get(0).getDistancePrice().doubleValue());
                    flowServiceTypePrices.get(0).setNote("路程"+distance+"km,计费("+flowServiceTypePrices.get(0).getDistancePrice().doubleValue()+"/1km)");
                }else{
                    distancePrice = flowServiceTypePrices.get(0).getDistancePrice().doubleValue();
                    flowServiceTypePrices.get(0).setTotalPrice(flowServiceTypePrices.get(0).getDistanceEnd().doubleValue() * flowServiceTypePrices.get(0).getDistancePrice().doubleValue());
                    flowServiceTypePrices.get(0).setNote("路程"+flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()+"km内，按"+flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()+"km计费("+flowServiceTypePrices.get(0).getDistancePrice().doubleValue()+"/"+flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()+"km)");
                }
            }else {
                if(flowServiceTypePrices.get(0).getType().intValue() == 0){
                    distancePrice = distance * flowServiceTypePrices.get(0).getDistancePrice().doubleValue();
                    flowServiceTypePrices.get(0).setTotalPrice(distance * flowServiceTypePrices.get(0).getDistancePrice().doubleValue());
                    flowServiceTypePrices.get(0).setNote("路程"+distance+"km,计费("+flowServiceTypePrices.get(0).getDistancePrice().doubleValue()+"/1km)");
                }else {
                    distancePrice = (distance - flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()) * (flowServiceTypePrices.get(0).getDistancePrice().doubleValue() / flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()) + flowServiceTypePrices.get(0).getDistancePrice().doubleValue();
                    flowServiceTypePrices.get(0).setTotalPrice(flowServiceTypePrices.get(0).getDistanceEnd().doubleValue() * flowServiceTypePrices.get(0).getDistancePrice().doubleValue());
                    flowServiceTypePrices.get(0).setNote("路程"+flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()+"km内，按"+flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()+"km计费("+(flowServiceTypePrices.get(0).getDistancePrice().doubleValue() / flowServiceTypePrices.get(0).getDistanceEnd().doubleValue()) +"/1km)");
                }
            }
            result.add(flowServiceTypePrices.get(0));
        }else {
            //已经计算的路程
            //从开始计算到结尾
            for (int i = 0;i<flowServiceTypePrices.size();i++){
                //当前阶梯计算的距离
                double d;
                //当大于结束时候
                if(distance >= flowServiceTypePrices.get(i).getDistanceEnd().doubleValue() + flowServiceTypePrices.get(i).getDistanceBegin().doubleValue()){
                    //直接计算结束，计算结束到开始路程
                    if(i == flowServiceTypePrices.size() - 1){
                        d = distance - flowServiceTypePrices.get(i).getDistanceBegin().doubleValue();
                    }else{
                        d = flowServiceTypePrices.get(i).getDistanceEnd().doubleValue() - flowServiceTypePrices.get(i).getDistanceBegin().doubleValue();
                    }
                    if(flowServiceTypePrices.get(i).getType().intValue() == 0){
                        distancePrice += d * flowServiceTypePrices.get(i).getDistancePrice().doubleValue();
                        flowServiceTypePrices.get(i).setNote("路程"+d+"km，计费("+flowServiceTypePrices.get(i).getDistancePrice().doubleValue()+"/1km)");
                        flowServiceTypePrices.get(i).setTotalPrice(d * flowServiceTypePrices.get(i).getDistancePrice().doubleValue());
                    }else{
                        distancePrice += flowServiceTypePrices.get(i).getDistancePrice().doubleValue();
                        flowServiceTypePrices.get(i).setNote("路程"+d+"km，计费("+flowServiceTypePrices.get(i).getDistancePrice().doubleValue()+"/"+flowServiceTypePrices.get(i).getDistanceEnd().doubleValue()+"km)");
                        flowServiceTypePrices.get(i).setTotalPrice(flowServiceTypePrices.get(i).getDistancePrice().doubleValue());
                    }
                    result.add(flowServiceTypePrices.get(i));
                }else if(distance >= flowServiceTypePrices.get(i).getDistanceBegin().doubleValue()){
                    //如果大于初始，小于结束时候
                    if(flowServiceTypePrices.get(i).getType().intValue() == 0){
                        d = distance - flowServiceTypePrices.get(i).getDistanceBegin().doubleValue();
                        flowServiceTypePrices.get(i).setNote("路程"+d+"km，计费("+flowServiceTypePrices.get(i).getDistancePrice().doubleValue()+"/1km)");
                        flowServiceTypePrices.get(i).setTotalPrice(d * flowServiceTypePrices.get(i).getDistancePrice().doubleValue());
                        distancePrice += d * flowServiceTypePrices.get(i).getDistancePrice().doubleValue();
                    }else {
                        d = flowServiceTypePrices.get(i).getDistanceEnd().doubleValue() - flowServiceTypePrices.get(i).getDistanceBegin().doubleValue();
                        flowServiceTypePrices.get(i).setNote("路程"+flowServiceTypePrices.get(i).getDistanceEnd().doubleValue()+"km内，按"+flowServiceTypePrices.get(i).getDistanceEnd().doubleValue()+"km计费("+flowServiceTypePrices.get(i).getDistancePrice().doubleValue()+"/"+flowServiceTypePrices.get(i).getDistanceEnd().doubleValue()+"km)");
                        flowServiceTypePrices.get(i).setTotalPrice(flowServiceTypePrices.get(i).getDistancePrice().doubleValue());
                        distancePrice += flowServiceTypePrices.get(i).getDistancePrice().doubleValue();
                    }
                    result.add(flowServiceTypePrices.get(i));
                }
                //小于就不计算
            }
        }

        Map<String,Object> res = new HashMap<>();
        res.put("distance",distance);
        res.put("distancePrice",distancePrice);
        res.put("dis",result);
        return ResponseData.success(res);
    }

    //百度登录接口
    //用户登录接口
    @RequestMapping("/bdLogin")
    @ResponseBody
    public ResponseData bdLogin(HttpServletRequest request) throws Exception {
        //修改成获取手机号登录
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String authCode = baseRequest.getString("authCode");
        if(authCode.equals("")){
            return ResponseData.error("请先授权登录");
        }

        Map<String,String> session = BdUtil.sessionKey(authCode);
        if(session == null || session.containsKey("errno")){
            return ResponseData.error("授权失败");
        }
        String sessionKey = session.get("session_key");
        String openId = session.get("openid");
        String iv = baseRequest.getString("iv");
        String nickName = baseRequest.getString("nickName");
        String faceUrl = baseRequest.getString("faceUrl");
        String encryptedData = baseRequest.getString("encryptedData");
        //如果encryptedData不为空，获取手机号
        String mobile = "";
        if(!encryptedData.equals("")){
            try{
                BdSecurityUtil bdSecurityUtil = new BdSecurityUtil();

                String pain = bdSecurityUtil.decrypt(encryptedData, sessionKey, iv);
                Map<String,String> res = JSON.parseObject(pain,Map.class);
                if(res.containsKey("mobile")){
                    mobile = res.get("mobile");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //成功，返回openid
        //查询数据库是否存在该用户
        QueryWrapper<FlowBdMember> flowBdMemberQueryWrapper = new QueryWrapper<>();
        flowBdMemberQueryWrapper.eq("bd_openid",openId);
        FlowBdMember flowBdMember = flowBdMemberService.getOne(flowBdMemberQueryWrapper);
        if(flowBdMember == null){
            //添加
            flowBdMember = new FlowBdMember();
            flowBdMember.setCreateTime(new Date());
            flowBdMember.setFaceUrl(faceUrl);
            flowBdMember.setNickName(nickName);
            flowBdMember.setAccount(mobile);
            flowBdMember.setUpdateTime(new Date());
            flowBdMember.setBdOpenid(openId);
            flowBdMember.setIsNew(0);
            boolean result = flowBdMemberService.save(flowBdMember);
            if(result){
                if(!mobile.equals("")) {
                    //领取卷
                    QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                    flowMemberCouponActivityQueryWrapper.eq("status", 0);
                    List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                    if (couponActivities.size() != 0) {
                        //查询该账户的历史领取
                        List<Long> ids = new ArrayList<>();

                        for (int i = 0; i < couponActivities.size(); i++) {
                            ids.add(couponActivities.get(i).getCouponActivityId());
                        }

                        QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                        flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                        flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowBdMember.getMemberId());
                        flowMemberCouponActivityRecordQueryWrapper.eq("type",1);
                        List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                        for (int i = 0; i < couponActivities.size(); i++) {
                            boolean get = false;
                            for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                                if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                    get = true;
                                }
                            }
                            if (!get) {
                                //领取
                                FlowCoupon flowCoupon = new FlowCoupon();
                                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                                flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                                flowCoupon.setCreateTime(new Date());
                                flowCoupon.setIsGet(1);
                                flowCoupon.setMemberId(flowBdMember.getMemberId());
                                flowCoupon.setUpdateTime(new Date());
                                flowCoupon.setGetTime(new Date());
                                flowCoupon.setIsUsed(0);
                                flowCoupon.setType(1);
                                flowCoupon.setUseTime(new Date());
                                flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                                flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                                flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                                if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                    flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                                } else {
                                    flowCoupon.setEndTime(new Date());
                                }
                                flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                                flowCouponService.save(flowCoupon);
                                //领取记录
                                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                                flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                                flowMemberCouponActivityRecord.setCreateTime(new Date());
                                flowMemberCouponActivityRecord.setMemberId(flowBdMember.getMemberId());
                                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                                flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                            }
                        }
                    }
                    //结束
                    FlowBdMemberParam flowBdMemberParam = new FlowBdMemberParam();
                    flowBdMemberParam.setIsNew(1);
                    flowBdMemberParam.setMemberId(flowBdMember.getMemberId());
                    flowBdMemberService.update(flowBdMemberParam);
                }
                //
                return ResponseData.success(flowBdMember.getBdOpenid());
            }else {
                return ResponseData.error("登录失败");
            }
        }else{
            if(!mobile.equals("")){
                flowBdMember.setAccount(mobile);
            }
            flowBdMember.setNickName(nickName);
            flowBdMember.setFaceUrl(faceUrl);
            flowBdMemberService.updateById(flowBdMember);
            //查询是否已经绑定过
            if(!mobile.equals("") && flowBdMember.getIsNew().intValue() == 0) {
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                flowMemberCouponActivityQueryWrapper.eq("status", 0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if (couponActivities.size() != 0) {
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0; i < couponActivities.size(); i++) {
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowBdMember.getMemberId());
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",1);
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0; i < couponActivities.size(); i++) {
                        boolean get = false;
                        for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                            if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                get = true;
                            }
                        }
                        if (!get) {
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowBdMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setType(1);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                            } else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowBdMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束

                //结束
                FlowBdMemberParam flowBdMemberParam = new FlowBdMemberParam();
                flowBdMemberParam.setIsNew(1);
                flowBdMemberParam.setMemberId(flowBdMember.getMemberId());
                flowBdMemberService.update(flowBdMemberParam);
            }
            //
            return ResponseData.success(flowBdMember.getBdOpenid());
        }
    }

    @RequestMapping("/wxLogin")
    @ResponseBody
    public ResponseData wxLogin(HttpServletRequest request) throws Exception {
        //修改成获取手机号登录
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String authCode = baseRequest.getString("authCode");
        if(authCode.equals("")){
            return ResponseData.error("请先授权登录");
        }

        String openId = "";
        String sessionKey = "";
        String unionid = "";
        if(!authCode.equals("")){
            Jscode2Session wxtoken = WeiXinUtil.getJsCodeSession(authCode);
            if(wxtoken != null && wxtoken.getOpenid() != null && !wxtoken.getOpenid().equals("")){
                openId = wxtoken.getOpenid();
                sessionKey = wxtoken.getSession_key();
                System.out.println("wxtoken:"+wxtoken);
                unionid=wxtoken.getUnionid();
                System.out.println("unionid:"+unionid);

            }
        }else {
            return ResponseData.error("请先授权登录");
        }

        String iv = baseRequest.getString("iv");
        String nickName = baseRequest.getString("nickName");
        String faceUrl = baseRequest.getString("faceUrl");
        String encryptedData = baseRequest.getString("encryptedData");
        //如果encryptedData不为空，获取手机号
        String mobile = "";
        if(!encryptedData.equals("")){
            try{
                WxPhoneData result = WeiXinUtil.phones(sessionKey,iv,encryptedData);
                if(result != null && result.getPhoneNumber() != null){
                    mobile = result.getPhoneNumber();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //成功，返回openid
        //查询数据库是否存在该用户
        QueryWrapper<FlowWxMember> flowWxMemberQueryWrapper = new QueryWrapper<>();
        flowWxMemberQueryWrapper.eq("wx_openid",openId);
        FlowWxMember flowWxMember = flowWxMemberService.getOne(flowWxMemberQueryWrapper);
        if(flowWxMember == null){
            //添加
            flowWxMember = new FlowWxMember();
            flowWxMember.setCreateTime(new Date());
            flowWxMember.setFaceUrl(faceUrl);
            flowWxMember.setNickName(nickName);
            flowWxMember.setAccount(mobile);
            flowWxMember.setUpdateTime(new Date());
            flowWxMember.setWxOpenid(openId);
            flowWxMember.setIsNew(0);
            flowWxMember.setWxUnionid(unionid);
            boolean result = flowWxMemberService.save(flowWxMember);
            //发送新用户通知短信
            if(StringUtils.isEmpty(nickName)){
                nickName="xxx";
            }
            this.sendRefundMsg(nickName,mobile);
            if(result){
                if(!mobile.equals("")) {
                    //领取卷
                    QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                    flowMemberCouponActivityQueryWrapper.eq("status", 0);
                    List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                    if (couponActivities.size() != 0) {
                        //查询该账户的历史领取
                        List<Long> ids = new ArrayList<>();

                        for (int i = 0; i < couponActivities.size(); i++) {
                            ids.add(couponActivities.get(i).getCouponActivityId());
                        }

                        QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                        flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                        flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowWxMember.getMemberId());
                        flowMemberCouponActivityRecordQueryWrapper.eq("type",2);
                        List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                        for (int i = 0; i < couponActivities.size(); i++) {
                            boolean get = false;
                            for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                                if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                    get = true;
                                }
                            }
                            if (!get) {
                                //领取
                                FlowCoupon flowCoupon = new FlowCoupon();
                                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                                flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                                flowCoupon.setCreateTime(new Date());
                                flowCoupon.setIsGet(1);
                                flowCoupon.setMemberId(flowWxMember.getMemberId());
                                flowCoupon.setUpdateTime(new Date());
                                flowCoupon.setGetTime(new Date());
                                flowCoupon.setIsUsed(0);
                                flowCoupon.setType(2);
                                flowCoupon.setUseTime(new Date());
                                flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                                flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                                flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                                if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                    flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                                } else {
                                    flowCoupon.setEndTime(new Date());
                                }
                                flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                                flowCouponService.save(flowCoupon);
                                //领取记录
                                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                                flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                                flowMemberCouponActivityRecord.setCreateTime(new Date());
                                flowMemberCouponActivityRecord.setMemberId(flowWxMember.getMemberId());
                                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                                flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                            }
                        }
                    }
                    //结束
                    FlowWxMemberParam flowWxMemberParam = new FlowWxMemberParam();
                    flowWxMemberParam.setIsNew(1);
                    flowWxMemberParam.setMemberId(flowWxMember.getMemberId());
                    flowWxMemberService.update(flowWxMemberParam);
                }

                return ResponseData.success(flowWxMember.getWxOpenid());
            }else {
                return ResponseData.error("登录失败");
            }
        }else{
            if(!mobile.equals("")){
                flowWxMember.setAccount(mobile);
            }
//            flowWxMember.setNickName(nickName);
            flowWxMember.setFaceUrl(faceUrl);
            flowWxMember.setWxUnionid(unionid);
            flowWxMemberService.updateById(flowWxMember);
            //查询是否已经绑定过
            if(!mobile.equals("") && flowWxMember.getIsNew().intValue() == 0) {
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                flowMemberCouponActivityQueryWrapper.eq("status", 0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if (couponActivities.size() != 0) {
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0; i < couponActivities.size(); i++) {
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowWxMember.getMemberId());
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",2);
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0; i < couponActivities.size(); i++) {
                        boolean get = false;
                        for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                            if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                get = true;
                            }
                        }
                        if (!get) {
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowWxMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setType(2);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                            } else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowWxMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束

                //结束
                FlowWxMemberParam flowWxMemberParam = new FlowWxMemberParam();
                flowWxMemberParam.setIsNew(1);
                flowWxMemberParam.setMemberId(flowWxMember.getMemberId());
                flowWxMemberService.update(flowWxMemberParam);
            }
            //
            return ResponseData.success(flowWxMember.getWxOpenid());
        }
    }

    @RequestMapping("/bdPhoneLogin")
    @ResponseBody
    public ResponseData bdPhoneLogin(HttpServletRequest request) throws AlipayApiException {
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String phone = baseRequest.getString("phone");
        String authCode = baseRequest.getString("authCode");
        String code = baseRequest.getString("code");
        if(code.equals("")){
            return ResponseData.error("请输入手机验证码");
        }

        //查询手机验证码
        QueryWrapper<FlowPhoneCode> flowPhoneCodeQueryWrapper = new QueryWrapper<>();
        flowPhoneCodeQueryWrapper.eq("phone",phone);
        flowPhoneCodeQueryWrapper.apply("create_time >= DATE_SUB(NOW(),INTERVAL 5 MINUTE)");
        flowPhoneCodeQueryWrapper.orderByDesc("create_time");
        flowPhoneCodeQueryWrapper.last("limit 1");
        FlowPhoneCode flowPhoneCode = flowPhoneCodeService.getOne(flowPhoneCodeQueryWrapper);
        if(flowPhoneCode == null){
            return ResponseData.error("请重新获取验证码");
        }
        if(!code.equals(flowPhoneCode.getCode())){
            return ResponseData.error("验证码错误");
        }

        Map<String,String> session = BdUtil.sessionKey(authCode);
        if(session == null || session.containsKey("errno")){
            return ResponseData.error("授权失败");
        }
        String openId = session.get("openid");

        //查询
        QueryWrapper<FlowBdMember> flowBdMemberQueryWrapper = new QueryWrapper<>();
        flowBdMemberQueryWrapper.eq("bd_openid",openId);
        FlowBdMember flowBdMember = flowBdMemberService.getOne(flowBdMemberQueryWrapper);
        if(flowBdMember == null){
            flowBdMember = new FlowBdMember();
            flowBdMember.setAccount(phone);
            flowBdMember.setCreateTime(new Date());
            flowBdMember.setFaceUrl("");
            flowBdMember.setNickName("");
            flowBdMember.setUpdateTime(new Date());
            flowBdMember.setBdOpenid(openId);
            flowBdMember.setIsNew(1);
            boolean result = flowBdMemberService.save(flowBdMember);
            if(result){
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
                flowMemberCouponActivityQueryWrapper.eq("status",0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if(couponActivities.size() != 0){
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0;i<couponActivities.size();i++){
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id",ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",1);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowBdMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0;i<couponActivities.size();i++){
                        boolean get = false;
                        for (int j = 0;j<flowMemberCouponActivityRecords.size();j++){
                            if(couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()){
                                get = true;
                            }
                        }
                        if(!get){
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowBdMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setType(1);
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if(couponActivities.get(i).getOutDate().intValue() > 0){
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(),couponActivities.get(i).getOutDate().intValue()));
                            }else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowBdMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束
                return ResponseData.success(flowBdMember.getBdOpenid());
            }else {
                return ResponseData.error("登录失败");
            }
        }else {
            flowBdMember.setAccount(phone);
            flowBdMemberService.updateById(flowBdMember);
            if(flowBdMember.getIsNew().intValue() == 0){
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
                flowMemberCouponActivityQueryWrapper.eq("status",0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if(couponActivities.size() != 0){
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0;i<couponActivities.size();i++){
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id",ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",1);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowBdMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0;i<couponActivities.size();i++){
                        boolean get = false;
                        for (int j = 0;j<flowMemberCouponActivityRecords.size();j++){
                            if(couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()){
                                get = true;
                            }
                        }
                        if(!get){
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setType(1);
                            flowCoupon.setMemberId(flowBdMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if(couponActivities.get(i).getOutDate().intValue() > 0){
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(),couponActivities.get(i).getOutDate().intValue()));
                            }else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowBdMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束

                FlowBdMemberParam flowBdMemberParam = new FlowBdMemberParam();
                flowBdMemberParam.setIsNew(1);
                flowBdMemberParam.setMemberId(flowBdMember.getMemberId());
                flowBdMemberService.update(flowBdMemberParam);
            }

            return ResponseData.success(flowBdMember.getBdOpenid());
        }
    }

    @RequestMapping("/wxPhoneLogin")
    @ResponseBody
    public ResponseData wxPhoneLogin(HttpServletRequest request) throws AlipayApiException {
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String phone = baseRequest.getString("phone");
        String authCode = baseRequest.getString("authCode");
        String code = baseRequest.getString("code");
        if(code.equals("")){
            return ResponseData.error("请输入手机验证码");
        }

        //查询手机验证码
        QueryWrapper<FlowPhoneCode> flowPhoneCodeQueryWrapper = new QueryWrapper<>();
        flowPhoneCodeQueryWrapper.eq("phone",phone);
        flowPhoneCodeQueryWrapper.apply("create_time >= DATE_SUB(NOW(),INTERVAL 5 MINUTE)");
        flowPhoneCodeQueryWrapper.orderByDesc("create_time");
        flowPhoneCodeQueryWrapper.last("limit 1");
        FlowPhoneCode flowPhoneCode = flowPhoneCodeService.getOne(flowPhoneCodeQueryWrapper);
        if(flowPhoneCode == null){
            return ResponseData.error("请重新获取验证码");
        }
        if(!code.equals(flowPhoneCode.getCode())){
            return ResponseData.error("验证码错误");
        }

        String openId = "";
        if(!authCode.equals("")){
            Jscode2Session wxtoken = WeiXinUtil.getJsCodeSession(authCode);
            if(wxtoken != null && wxtoken.getOpenid() != null && !wxtoken.getOpenid().equals("")){
                openId = wxtoken.getOpenid();
            }
        }else {
            return ResponseData.error("请先授权登录");
        }

        //查询
        QueryWrapper<FlowWxMember> flowWxMemberQueryWrapper = new QueryWrapper<>();
        flowWxMemberQueryWrapper.eq("wx_openid",openId);
        FlowWxMember flowWxMember = flowWxMemberService.getOne(flowWxMemberQueryWrapper);
        if(flowWxMember == null){
            flowWxMember = new FlowWxMember();
            flowWxMember.setAccount(phone);
            flowWxMember.setCreateTime(new Date());
            flowWxMember.setFaceUrl("");
            flowWxMember.setNickName("");
            flowWxMember.setUpdateTime(new Date());
            flowWxMember.setWxOpenid(openId);
            flowWxMember.setIsNew(1);
            boolean result = flowWxMemberService.save(flowWxMember);
            if(result){
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
                flowMemberCouponActivityQueryWrapper.eq("status",0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if(couponActivities.size() != 0){
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0;i<couponActivities.size();i++){
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id",ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",2);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowWxMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0;i<couponActivities.size();i++){
                        boolean get = false;
                        for (int j = 0;j<flowMemberCouponActivityRecords.size();j++){
                            if(couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()){
                                get = true;
                            }
                        }
                        if(!get){
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowWxMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setType(2);
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if(couponActivities.get(i).getOutDate().intValue() > 0){
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(),couponActivities.get(i).getOutDate().intValue()));
                            }else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowWxMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束
                return ResponseData.success(flowWxMember.getWxOpenid());
            }else {
                return ResponseData.error("登录失败");
            }
        }else {
            flowWxMember.setAccount(phone);
            flowWxMemberService.updateById(flowWxMember);
            if(flowWxMember.getIsNew().intValue() == 0){
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
                flowMemberCouponActivityQueryWrapper.eq("status",0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if(couponActivities.size() != 0){
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0;i<couponActivities.size();i++){
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id",ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",2);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id",flowWxMember.getMemberId());
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0;i<couponActivities.size();i++){
                        boolean get = false;
                        for (int j = 0;j<flowMemberCouponActivityRecords.size();j++){
                            if(couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()){
                                get = true;
                            }
                        }
                        if(!get){
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setType(2);
                            flowCoupon.setMemberId(flowWxMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if(couponActivities.get(i).getOutDate().intValue() > 0){
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(),couponActivities.get(i).getOutDate().intValue()));
                            }else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowWxMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束

                FlowWxMemberParam flowWxMemberParam = new FlowWxMemberParam();
                flowWxMemberParam.setIsNew(1);
                flowWxMemberParam.setMemberId(flowWxMember.getMemberId());
                flowWxMemberService.update(flowWxMemberParam);
            }

            return ResponseData.success(flowWxMember.getWxOpenid());
        }
    }


    @RequestMapping("/flowStoreRegion")
    @ResponseBody
    public LayuiPageInfo flowStoreRegion(FlowStoreRegionParam flowStoreRegionParam){
        return this.flowStoreRegionService.findPageBySpec(flowStoreRegionParam);

    }

    @RequestMapping("/flowStoreRegionPrice")
    @ResponseBody
    public LayuiPageInfo flowStoreRegion(FlowStoreRegionPriceParam flowStoreRegionPriceParam){
        return this.flowStoreRegionPriceService.findPageBySpec(flowStoreRegionPriceParam);

    }

    @RequestMapping("/flowServiceAreaPrice")
    @ResponseBody
    public LayuiPageInfo flowServiceAreaPrice(FlowServiceAreaPriceParam flowServiceAreaPriceParam){
        return this.flowServiceAreaPriceService.findPageBySpec(flowServiceAreaPriceParam);

    }

    @RequestMapping("/flowStoreCityList")
    @ResponseBody
    public LayuiPageInfo list(FlowStoreCityParam flowStoreCityParam) {
        return this.flowStoreCityService.findPageBySpec(flowStoreCityParam);
    }

    /**
     * 根据总公司配置发送的短信
     * @param
     */
    public void sendRefundMsg(String name,String number){
        //查询服务的网点
        try{

                //查询总公司网点
                QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
                deptQueryWrapper.eq("pid",0);
                deptQueryWrapper.last("limit 1");
                Dept dept = deptService.getOne(deptQueryWrapper);
                if(dept != null){
                    //查询该客服人员
                    QueryWrapper<FlowDeptPhones> flowDeptPhonesQueryWrapper = new QueryWrapper<>();
                    flowDeptPhonesQueryWrapper.eq("dept_id",dept.getDeptId());
                    List<FlowDeptPhones> flowDeptPhones = flowDeptPhonesService.list(flowDeptPhonesQueryWrapper);
                    if(flowDeptPhones.size() > 0){
                        Random random = new Random();
                        int n = random.nextInt(flowDeptPhones.size());
                        AliyuncsUtil.sendSms(7,flowDeptPhones.get(n).getMobile(),name,number);
                    }
                }

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    //获取用户可领取的优惠卷
    @RequestMapping("/showCoupons")
    @ResponseBody
    public ResponseData showCoupons(HttpServletRequest request){
//        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
//        int type = baseRequest.getInt("type");
        QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
        flowMemberCouponActivityQueryWrapper.eq("activity_type", 1);
        flowMemberCouponActivityQueryWrapper.eq("status", 0);
        List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
        List<FlowMemberCouponActivity> result=new ArrayList<>();
        for (FlowMemberCouponActivity i:couponActivities){
            Date endDate=DateUtil.addDay(i.getCreateTime(),i.getOutDate().intValue());
            if(DateUtil.compareDate(endDate,new Date()) < 0){
                result.add(i);
            }

        }
        return ResponseData.success(result);
    }

    //小程序领卷
    @RequestMapping("/receiveCoupons")
    @ResponseBody
    public ResponseData receiveCoupons(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        Integer type=baseRequest.getInt("type");
        String ids=baseRequest.getString("couponActivityId");
        List idList=new ArrayList();
        if(ids.indexOf("/")>0){
            String arr[]=ids.split("/");
            for(String s:arr){
                if(!StringUtils.isEmpty(s)){
                    idList.add(s);

                }
            }
        }else {
            idList.add(ids);
        }

        for(int i=0;i<idList.size();i++){

            System.out.println("sssssssss"+ids);

            FlowMemberCouponActivity detail = this.flowMemberCouponActivityService.getById(idList.get(i).toString());

            //查询用户是否已领取改卷

            QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
            flowMemberCouponActivityRecordQueryWrapper.eq("coupon_activity_id",detail.getCouponActivityId());
            if(!StringUtils.isEmpty(type)&&type==0){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",0);

            }else if(!StringUtils.isEmpty(type)&&type==1){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",1);

            }else if(!StringUtils.isEmpty(type)&&type==2){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",2);

            }else {
                flowMemberCouponActivityRecordQueryWrapper.eq("type",MEMBER_TYPE_DOUYIN);
                type=MEMBER_TYPE_DOUYIN;
            }

            flowMemberCouponActivityRecordQueryWrapper.eq("member_id",baseRequest.getLong("memberId"));
            int count = flowMemberCouponActivityRecordService.count(flowMemberCouponActivityRecordQueryWrapper);
            if(count == 0){
                //领取
                FlowCoupon flowCoupon = new FlowCoupon();
                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                flowCoupon.setCouponTitle(detail.getActivityTitle());
                flowCoupon.setCreateTime(new Date());
                flowCoupon.setIsGet(1);
                flowCoupon.setType(type);
                flowCoupon.setMemberId(baseRequest.getLong("memberId"));
                flowCoupon.setUpdateTime(new Date());
                flowCoupon.setGetTime(new Date());
                flowCoupon.setIsUsed(0);
                flowCoupon.setUseTime(new Date());
                flowCoupon.setOutDay(detail.getOutDate());
                flowCoupon.setMoneys(detail.getPrice());
                flowCoupon.setOnprice(detail.getOnprice());
                if(detail.getOutDate().intValue() > 0){
                    flowCoupon.setEndTime(DateUtil.addDay(new Date(),detail.getOutDate().intValue()));
                }else {
                    flowCoupon.setEndTime(new Date());
                }
                flowCoupon.setServiceType(detail.getServiceType());
                flowCouponService.save(flowCoupon);
                //领取记录
                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                flowMemberCouponActivityRecord.setCouponActivityId(detail.getCouponActivityId());
                flowMemberCouponActivityRecord.setCreateTime(new Date());
                flowMemberCouponActivityRecord.setMemberId(baseRequest.getLong("memberId"));
                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                flowMemberCouponActivityRecord.setType(type);
                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
            }else {
                return ResponseData.error("已领取");
            }
        }
        return ResponseData.success("操作成功");
    }

    //核销优惠卷
    @RequestMapping("/eliminate")
    @ResponseBody
    public ResponseData eliminate(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);

        String id=baseRequest.getString("couponId");
        FlowCoupon detail=flowCouponService.getById(id);
        if(detail!=null){

            FlowCouponParam flowCouponParam=new FlowCouponParam();
            flowCouponParam.setCouponId(detail.getCouponId());
            flowCouponParam.setIsUsed(1);
            flowCouponService.update(flowCouponParam);
        }
        return ResponseData.success("操作成功");
    }

    //对比用户已领取的优惠卷
    @RequestMapping("/inspectCoupons")
    @ResponseBody
    public ResponseData inspectCoupons(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String couponActivityId=baseRequest.getString("couponActivityId");
        Long memberId=baseRequest.getLong("memberId");
        Long type=baseRequest.getLong("type");

        List list=new ArrayList();
        if(couponActivityId.indexOf("/")>0){
            String  arr[]=couponActivityId.split("/");
            for(String id:arr){
                if (!StringUtils.isEmpty(id)){
                    list.add(id);
                }
            }
        }else {
            list.add(couponActivityId);
        }

        List result=new ArrayList();

        for(int s=0;s<list.size();s++){


            QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
            flowMemberCouponActivityRecordQueryWrapper.eq("coupon_activity_id",Long.valueOf(list.get(s).toString()));
            flowMemberCouponActivityRecordQueryWrapper.eq("member_id",memberId);
            if(!StringUtils.isEmpty(type)&&type==0){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",0);

            }else if(!StringUtils.isEmpty(type)&&type==1){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",1);

            }else if(!StringUtils.isEmpty(type)&&type==2){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",2);
            }else {
                flowMemberCouponActivityRecordQueryWrapper.eq("type",MEMBER_TYPE_DOUYIN);
            }
            int count = flowMemberCouponActivityRecordService.count(flowMemberCouponActivityRecordQueryWrapper);
            if(count!=0){
                result.add(list.get(s).toString());
            }
        }
        return ResponseData.success(result);
    }


    //微信小程序优惠界面
    @RequestMapping("/newCouponActivitys")
    @ResponseBody
    public ResponseData newCouponActivitys(HttpServletRequest request){
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        Long memberId=baseRequest.getLong("memberId");
        Long type=baseRequest.getLong("type");

        QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
        flowMemberCouponActivityQueryWrapper.eq("status",0);
        //flowMemberCouponActivityQueryWrapper.eq("activity_type",0);
        List<FlowMemberCouponActivity> flowMemberCouponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
        List<FlowMemberCouponActivity> result=new ArrayList<>();
        for (FlowMemberCouponActivity i:flowMemberCouponActivities){
            Date endDate=DateUtil.addDay(i.getCreateTime(),i.getOutDate().intValue());
            if(DateUtil.compareDate(endDate,new Date()) < 0||i.getOutDate()<1){
                result.add(i);
            }

        }

        for(FlowMemberCouponActivity f:result){
            QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
            flowMemberCouponActivityRecordQueryWrapper.eq("member_id",memberId);
            if(!StringUtils.isEmpty(type)&&type==0){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",0);

            }else if(!StringUtils.isEmpty(type)&&type==1){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",1);

            }else if(!StringUtils.isEmpty(type)&&type==2){
                flowMemberCouponActivityRecordQueryWrapper.eq("type",2);

            }else {
                flowMemberCouponActivityRecordQueryWrapper.eq("type",MEMBER_TYPE_DOUYIN);
            }

            flowMemberCouponActivityRecordQueryWrapper.eq("coupon_activity_id",f.getCouponActivityId());
            int count = flowMemberCouponActivityRecordService.count(flowMemberCouponActivityRecordQueryWrapper);
            if(count!=0){
                f.setIsGet(1);
            }else {
                f.setIsGet(0);
            }

        }




        List<FlowService> flowServices = flowServiceService.list();
        for (int i = 0;i<result.size();i++){
            for (int j = 0;j<flowServices.size();j++){
                if(result.get(i).getServiceType().longValue() == flowServices.get(j).getServiceId().longValue()){
                    result.get(i).setServiceName(flowServices.get(j).getServiceName());
                }
            }
        }
        return ResponseData.success(result);
    }

    /**
     * 抖音登录
     *
     * @param request 请求
     * @return {@link ResponseData }
     * @throws Exception 例外
     */
    @RequestMapping("/dyLogin")
    @ResponseBody
    public ResponseData dyLogin(HttpServletRequest request) throws Exception {

        //修改成获取手机号登录
        HttpBaseRequest baseRequest = new HttpBaseRequest(request);
        String authCode = baseRequest.getString("authCode");
        String iv = baseRequest.getString("iv");
        String encryptedData = baseRequest.getString("encryptedData");
        if(StringUtils.isEmpty(authCode)){
            return ResponseData.error("authCode为空,请先授权登录");
        }
        if(StringUtils.isEmpty(iv)){
            return ResponseData.error("iv为空,请先授权登录");
        }
        if(StringUtils.isEmpty(encryptedData)){
            return ResponseData.error("encryptedData为空,请先授权登录");
        }

        String mobile="";
        String openId="";
        String unionid="";
        String nickName="";
        String faceUrl="";

        //调用接口,解析抖音用户的手机号
        DouyinResPhone douyinUserPhone = DouyinHttpUtil.getDouyinUserPhone(encryptedData, authCode, iv);
        if(douyinUserPhone==null){
            return ResponseData.error("登录失败");
        }
        else {
              mobile=douyinUserPhone.getPhoneNumber();
              openId=douyinUserPhone.getOpenid();
              unionid=douyinUserPhone.getUnionid();
        }


        //成功，返回openid
        //查询数据库是否存在该用户
        QueryWrapper<FlowDouyinMember> flowWxMemberQueryWrapper = new QueryWrapper<>();
        flowWxMemberQueryWrapper.eq("douyin_openid",openId);
        FlowDouyinMember flowDouyinMember = flowDouyinMemberService.getOne(flowWxMemberQueryWrapper);


        if(flowDouyinMember == null){
            //添加
            flowDouyinMember = new FlowDouyinMember();
            flowDouyinMember.setCreateTime(new Date());
            flowDouyinMember.setFaceUrl(faceUrl);
            flowDouyinMember.setNickName(nickName);
            flowDouyinMember.setAccount(mobile);
            flowDouyinMember.setUpdateTime(new Date());
            flowDouyinMember.setDouyinOpenid(openId);
            flowDouyinMember.setDyUnionid(unionid);
            flowDouyinMember.setIsNew(0);
            boolean result = flowDouyinMemberService.save(flowDouyinMember);
            //发送新用户通知短信
            if(StringUtils.isEmpty(nickName)){
                nickName="xxx";
            }
            this.sendRefundMsg(nickName,mobile);
            if(result){
                if(!mobile.equals("")) {
                    //领取卷
                    QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                    flowMemberCouponActivityQueryWrapper.eq("status", 0);
                    List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                    if (couponActivities.size() != 0) {
                        //查询该账户的历史领取
                        List<Long> ids = new ArrayList<>();

                        for (int i = 0; i < couponActivities.size(); i++) {
                            ids.add(couponActivities.get(i).getCouponActivityId());
                        }

                        QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                        flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                        flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowDouyinMember.getMemberId());
                        flowMemberCouponActivityRecordQueryWrapper.eq("type",MEMBER_TYPE_DOUYIN);
                        List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                        for (int i = 0; i < couponActivities.size(); i++) {
                            boolean get = false;
                            for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                                if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                    get = true;
                                }
                            }
                            if (!get) {
                                //领取
                                FlowCoupon flowCoupon = new FlowCoupon();
                                flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                                flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                                flowCoupon.setCreateTime(new Date());
                                flowCoupon.setIsGet(1);
                                flowCoupon.setMemberId(flowDouyinMember.getMemberId());
                                flowCoupon.setUpdateTime(new Date());
                                flowCoupon.setGetTime(new Date());
                                flowCoupon.setIsUsed(0);
                                flowCoupon.setType(MEMBER_TYPE_DOUYIN);
                                flowCoupon.setUseTime(new Date());
                                flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                                flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                                flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                                if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                    flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                                } else {
                                    flowCoupon.setEndTime(new Date());
                                }
                                flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                                flowCouponService.save(flowCoupon);
                                //领取记录
                                FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                                flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                                flowMemberCouponActivityRecord.setCreateTime(new Date());
                                flowMemberCouponActivityRecord.setMemberId(flowDouyinMember.getMemberId());
                                flowMemberCouponActivityRecord.setUpdateTime(new Date());
                                flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                                flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                            }
                        }
                    }
                    //结束
                    FlowDouyinMemberParam flowDouyinMemberParam = new FlowDouyinMemberParam();
                    flowDouyinMemberParam.setIsNew(1);
                    flowDouyinMemberParam.setMemberId(flowDouyinMember.getMemberId());
                    flowDouyinMemberService.update(flowDouyinMemberParam);
                }

                return ResponseData.success(flowDouyinMember.getDouyinOpenid());
            }else {
                return ResponseData.error("登录失败");
            }
        }else{
            if(!mobile.equals("")){
                flowDouyinMember.setAccount(mobile);
            }
//            flowWxMember.setNickName(nickName);
            flowDouyinMember.setFaceUrl(faceUrl);
            flowDouyinMember.setDyUnionid(unionid);
            flowDouyinMemberService.updateById(flowDouyinMember);
            //查询是否已经绑定过
            if(!mobile.equals("") && flowDouyinMember.getIsNew().intValue() == 0) {
                //领取卷
                QueryWrapper<FlowMemberCouponActivity> flowMemberCouponActivityQueryWrapper = new QueryWrapper<>();
                flowMemberCouponActivityQueryWrapper.eq("activity_type", 0);
                flowMemberCouponActivityQueryWrapper.eq("status", 0);
                List<FlowMemberCouponActivity> couponActivities = flowMemberCouponActivityService.list(flowMemberCouponActivityQueryWrapper);
                if (couponActivities.size() != 0) {
                    //查询该账户的历史领取
                    List<Long> ids = new ArrayList<>();

                    for (int i = 0; i < couponActivities.size(); i++) {
                        ids.add(couponActivities.get(i).getCouponActivityId());
                    }

                    QueryWrapper<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecordQueryWrapper = new QueryWrapper<>();
                    flowMemberCouponActivityRecordQueryWrapper.in("coupon_activity_id", ids);
                    flowMemberCouponActivityRecordQueryWrapper.eq("member_id", flowDouyinMember.getMemberId());
                    flowMemberCouponActivityRecordQueryWrapper.eq("type",MEMBER_TYPE_DOUYIN);
                    List<FlowMemberCouponActivityRecord> flowMemberCouponActivityRecords = flowMemberCouponActivityRecordService.list(flowMemberCouponActivityRecordQueryWrapper);
                    for (int i = 0; i < couponActivities.size(); i++) {
                        boolean get = false;
                        for (int j = 0; j < flowMemberCouponActivityRecords.size(); j++) {
                            if (couponActivities.get(i).getCouponActivityId().longValue() == flowMemberCouponActivityRecords.get(j).getCouponActivityId().longValue()) {
                                get = true;
                            }
                        }
                        if (!get) {
                            //领取
                            FlowCoupon flowCoupon = new FlowCoupon();
                            flowCoupon.setCouponCode(CommonUtil.getRanDomStringId());
                            flowCoupon.setCouponTitle(couponActivities.get(i).getActivityTitle());
                            flowCoupon.setCreateTime(new Date());
                            flowCoupon.setIsGet(1);
                            flowCoupon.setMemberId(flowDouyinMember.getMemberId());
                            flowCoupon.setUpdateTime(new Date());
                            flowCoupon.setGetTime(new Date());
                            flowCoupon.setIsUsed(0);
                            flowCoupon.setType(MEMBER_TYPE_DOUYIN);
                            flowCoupon.setUseTime(new Date());
                            flowCoupon.setOutDay(couponActivities.get(i).getOutDate());
                            flowCoupon.setMoneys(couponActivities.get(i).getPrice());
                            flowCoupon.setOnprice(couponActivities.get(i).getOnprice());
                            if (couponActivities.get(i).getOutDate().intValue() > 0) {
                                flowCoupon.setEndTime(DateUtil.addDay(new Date(), couponActivities.get(i).getOutDate().intValue()));
                            } else {
                                flowCoupon.setEndTime(new Date());
                            }
                            flowCoupon.setServiceType(couponActivities.get(i).getServiceType());
                            flowCouponService.save(flowCoupon);
                            //领取记录
                            FlowMemberCouponActivityRecord flowMemberCouponActivityRecord = new FlowMemberCouponActivityRecord();
                            flowMemberCouponActivityRecord.setCouponActivityId(couponActivities.get(i).getCouponActivityId());
                            flowMemberCouponActivityRecord.setCreateTime(new Date());
                            flowMemberCouponActivityRecord.setMemberId(flowDouyinMember.getMemberId());
                            flowMemberCouponActivityRecord.setUpdateTime(new Date());
                            flowMemberCouponActivityRecord.setType(flowCoupon.getType());
                            flowMemberCouponActivityRecordService.save(flowMemberCouponActivityRecord);
                        }
                    }
                }
                //结束

                //结束
                FlowDouyinMemberParam flowWxMemberParam = new FlowDouyinMemberParam();
                flowWxMemberParam.setIsNew(1);
                flowWxMemberParam.setMemberId(flowDouyinMember.getMemberId());
                flowDouyinMemberService.update(flowWxMemberParam);
            }
            //
            return ResponseData.success(flowDouyinMember.getDouyinOpenid());
        }
    }
}
